<?php
echo "<pre>";
/**
 * 
 * 根据微博采集数据生成专辑
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}

ini_set("display_errors",true);


define("CRON_NAME","cron_create_album_by_mblog");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",1000);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/album/inc.php");
error_reporting(E_ERROR);
class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
	
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		 $this->replace_into_data(1);
			
		$db_user_info =  new db_user_info();
		 
		 //获取tag的总数
		$count = $db_user_info->cron_get_user_count_no_album();

		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
		 $this->replace_into_data(0);
		
	}	
	
	function get_list_by_page($p)
	{
		$db_user_info =  new db_user_info();
		 
		 //获取tag的总数
		$list = $db_user_info->cron_get_user_list_no_album($p,PCOUNT);
		
		
		foreach($list as $key => $value)
		{
			$uid = $value["uid"];
				
			$this->create_album_by_uid($uid);
		
			
		}
		
		
		
	}
	
	
	function create_album_by_uid($uid)
	{
	
		$db_mblog_pic_content = new db_mblog_pic_content();
		$mblog_list = $db_mblog_pic_content->cron_get_mblog_list_order_by_create_time($uid,1,1000);
		
		
		
		$mblog_topic_array = array();
		foreach($mblog_list as $key => $value)//根据话题ID排列组
		{
			$mblog_topic_array[$value["topic_id"]][] = $value;
			
		}

		foreach($mblog_topic_array as $key => $value)
		{
			$this->do_with_topic_array($value,$uid,$key);
		
		}
		
		
		
		
	}
	
	
	function do_with_topic_array($topic_mblog_array,$uid,$topic_id)
	{
		if(!$topic_id || !$uid || !$topic_mblog_array)
		{
			return false;
		}
		$db_album_info = new db_album_info();
		$db_album_to_mblog = new db_album_to_mblog();
		
		$status_count = count($topic_mblog_array);
		if($status_count < 4)
		{
			$db_album_info = new db_album_info();
			$album_info_item = $db_album_info->cron_get_by_uid_and_topic_id($uid,$topic_id);
			$album_id = $album_info_item["id"];
			if($album_id)
			{
				$sql ="delete from album_info where id = {$album_id} ";
				$db_album_info->query($sql);
			}
			return false;
		}
		$min_value = 1000;
		$height = 250;//专辑大图250高度合适
		$good_key = 0;
	
		$linshi_value = $topic_mblog_array[0];
		unset($topic_mblog_array[0]);//提取出第一个，放到第一个位置（大图），后面的倒序排列
		$topic_mblog_array = array_reverse($topic_mblog_array);
		array_unshift($topic_mblog_array,$linshi_value);
		
		//创建专辑
		
		$title = $topic_mblog_array[0]["create_nickname"]."的".$topic_mblog_array[0]["topic"]."专辑";
		
		$mblog_show_type = $topic_mblog_array[0]["show_type"];
		$mblog_topic_id  = $topic_mblog_array[0]["topic_id"];
		$mblog_topic 	= $topic_mblog_array[0]["topic"];
		
		$db_album_info = new db_album_info();
		$album_info_item = $db_album_info->cron_get_by_uid_and_topic_id($uid,$topic_id);
		
		$db_user_info = new db_user_info();
		$user_item_info = $db_user_info->get_user_info_item_by_uid($uid);
		
		$max_time = "";
		foreach($topic_mblog_array as $value)
		{
			
			if($max_time < $value["create_time"])
			{
				$max_time = $value["create_time"];
			}
		}
		
		if(!$album_info_item)
		{
			$album_id = $db_album_info->cron_insert_album($title,$title,$uid,0,$body_url="",$body_attach="",$show_order,$mblog_show_type,$mblog_topic_id,$mblog_topic,$user_item_info["user_nickname"],$max_time,$user_item_info["sex"],$user_item_info["verified_type"]);
		}
		else
		{
			$album_id = $album_info_item["id"];
		}
		foreach($topic_mblog_array as $value)
		{
			$res = $db_album_to_mblog->insert_album_to_mblog($album_id,$value["id"],$value["create_time"]);
		}
		
		if(!$album_id)
		{
			return false;
		}
		
		
		
		
		$db_album_info->update_album_info_by_id($album_id,$uid,$title="",$desc="",$topic_mblog_array[0]["pic"],$topic_mblog_array[1]["pic"],$topic_mblog_array[2]["pic"],$topic_mblog_array[3]["pic"],$status_count,$status="",$show_type="",$body_url="",$body_attach="",$topic_mblog_array[0]["id"],$topic_mblog_array[1]["id"],$topic_mblog_array[2]["id"],$topic_mblog_array[3]["id"],
		$count_love="",$count_reply="",$count_good="",$level="",$show_order="",$mblog_show_type="",$mblog_topic_id="",$mblog_topic="",$user_item_info["user_nickname"],$max_time
		);

		
		
		$sql ="select count(*) as count from album_info where create_uid = {$uid}";
		$res = $db_album_info->select($sql);
		$count = $res[0]["count"];
		
		$sql ="update user_info set count_album = $count where uid = $uid";
		$db_album_info->query($sql);
		
	}
	
	
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
